/**
 * DaoSupport 数据访问层
 * @author hu_hengjiu
 * @date 2012-3-25
 */
package com.free.logic.dao;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.Entity;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

@Repository("dao") 
public class DaoSupport<T> extends HibernateDaoSupport implements DAO<T>{

	@Resource(name="hibernateTemplate")
	protected void setSuperHibernateTemplate(HibernateTemplate hibernateTemplate)
	{
		super.setHibernateTemplate(hibernateTemplate);
	}
	
	/**
	 * 保存实体对象
	 * @param entity 实体对象
	 * @return 主键值
	 */
	@Override
	public Serializable save(T entity)
	{
		return  getHibernateTemplate().save(entity);
	}
	
	/**
	 * 保存或更新实体对象
	 * @param entity 实体对象
	 */
	@Override
	public void saveOrUpdate(T entity)
	{
		getHibernateTemplate().saveOrUpdate(entity);
	}
	
	/**
	 * 保存或更新多个实体对象
	 * @param entities 实体对象集合
	 */
	@Override
	public void saveOrUpdate(Collection<T> entities)
	{
		getHibernateTemplate().saveOrUpdate(entities);
	}
	
	/**
	 * 删除实体对象
	 * @param entity 实体对象
	 */
	@Override
	public void delete(T entity)
	{
		getHibernateTemplate().delete(entity);
	}
	
	@Deprecated
	public void delete(T entity, String id)
	{
		
		Class<? extends Object> clazz = entity.getClass();
		String entityName = clazz.getAnnotation(Entity.class).name();
		
		getHibernateTemplate().execute(new HibernateCallback<T>(){

			@Override
			public T doInHibernate(Session session) throws HibernateException,
					SQLException {
				//session.cre
				return null;
			}
			
		});
		//getHibernateTemplate().delete(entityName, entityName)..delete(entityName, entity);
	}
	
	/**
	 * 根据主键查询
	 * @param entityClass
	 * @param pk
	 * @return
	 */
	@Override
	public T queryByPk(Class<T> entityClass, Object pk)
	{
		T entity = getHibernateTemplate().get(entityClass, new Integer(pk.toString()));
		return entity;
	}
	
	/**
	 * 根据主键删除
	 * @param entityClass
	 * @param pk
	 */
	@Override
	public void deleteByPk(Class<T> entityClass, Object pk)
	{
		T entity = queryByPk(entityClass, pk);
		getHibernateTemplate().delete(entity);
	}
	
	@SuppressWarnings("unchecked")
	public List<T> query(final String hql, final int pageNo,
		    final int pageSize)
	{
		List<T> result = new ArrayList<T>();
		List<?> list = getHibernateTemplate().executeFind(new HibernateCallback<List<?>>() {
			@Override
		    public List<?> doInHibernate(Session session)
		      throws HibernateException, SQLException {
		     Query query = session.createQuery(hql);
		     int offset = (pageNo-1) * pageSize;
		     query.setFirstResult(offset);
		     query.setMaxResults(pageSize);
		     List<?> list = query.list();
		     return list;
		    }
		   });
		for (Iterator<?> iter=list.iterator(); iter.hasNext();) {
	    	 T obj = (T)iter.next();
	    	 result.add(obj);
		}
		return result;
	}
	
	@Override
	public int queryResultCount(String queryHql)
	{
		return getHibernateTemplate().find(queryHql).size();
	}
}
